package org.loofer.ext.utils;

import android.text.TextUtils;
import org.loofer.ext.component.logger.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public final class PropertyUtils {
    private static final String CMD_GET_PROP = "getprop";
    public static final String PROPERTY_DNS_PRIMARY = "net.dns1";
    public static final String PROPERTY_DNS_SECONDARY = "net.dns2";
    private static final String TAG = "PropertyUtils";
    private static Class sClassSystemProperties;
    private static Method sMethodGetString;

    static {
        try {
            sClassSystemProperties = Class.forName("android.os.SystemProperties");
            sMethodGetString = sClassSystemProperties.getDeclaredMethod("get", new Class[]{String.class, String.class});
        } catch (Throwable e) {
            Logger.w(TAG, e);
        }
    }

    private PropertyUtils() {
    }

    public static String get(String key, String defValue) {
        if (TextUtils.isEmpty(key)) {
            return defValue;
        }
        String value = getWithReflect(key, null);
        if (TextUtils.isEmpty(value)) {
            value = getWithCmd(key, null);
        }
        if (TextUtils.isEmpty(value)) {
            value = defValue;
        }
        return value;
    }

    public static String getQuickly(String key, String defValue) {
        return TextUtils.isEmpty(key) ? defValue : getWithReflect(key, defValue);
    }

    private static String getWithReflect(String key, String defValue) {
        if (sClassSystemProperties == null || sMethodGetString == null) {
            return defValue;
        }
        String value = defValue;
        try {
            return (String) sMethodGetString.invoke(sClassSystemProperties, new Object[]{key, defValue});
        } catch (Throwable e) {
            Logger.w(TAG, e);
            return value;
        }
    }

    private static String getWithCmd(String key, String defValue) {
        Process process;
        BufferedReader reader;
        String value = defValue;
        try {
            process = Runtime.getRuntime().exec("getprop " + key);
            reader = null;
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(process.getInputStream()));
            try {
                StringBuilder builder = new StringBuilder();
                while (true) {
                    String line = reader2.readLine();
                    if (line == null) {
                        break;
                    }
                    builder.append(line);
                }
                String readValue = builder.toString();
                if (!TextUtils.isEmpty(readValue)) {
                    value = readValue;
                }
                if (reader2 != null) {
                    reader2.close();
                    reader = reader2;
                }
            } catch (IOException e) {
                reader = reader2;
                if (reader != null) {
                    reader.close();
                }
                process.destroy();
                return value;
            } catch (Throwable th) {
                Throwable th2 = th;
                reader = reader2;
                if (reader != null) {
                    reader.close();
                }
                throw th2;
            }
        } catch (IOException e2) {
            if (reader != null) {
                reader.close();
            }
            process.destroy();
        } catch (Throwable e3) {
            Logger.w(TAG, e3);
        }
        process.destroy();
        return value;
    }
}
